﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Drawing;
using System.Data;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using BiffenGUI.BiffenService;
using System.ServiceModel;

namespace BiffenGUI.UserControls
{
    public partial class MovieManagment : UserControl
    {
        private BiffenServiceClient service = null;
        private List<Movie> movies = null;

        public MovieManagment()
        {
            InitializeComponent();
        }

        public void SetServiceReferance(BiffenServiceClient svc)
        {
            service = svc;
            Initialize();
        }

        private void MovieManagment_Load(object sender, EventArgs e)
        {
            // MOVIES
            listMovies.DisplayMember = "Title";
            listMovies.ValueMember = "Id";
        }

        #region . GLOBAL .
        private void Initialize()
        {
            try
            {
                movies = service.GetAllMovies();
                UpdateMovieLists();
                LoadMovies();
            }
            catch (FaultException<BiffenServiceFault> ex)
            {
                MessageBox.Show("Fejl: " + ex.Detail.Message);
            }

        }

        private void UpdateMovieLists()
        {
            ClearMovieFields();

            listMovies.Items.Clear();
            listMovies.Items.AddRange(movies.ToArray());
        }
        #endregion

        #region . MOVIE .
        private int selectedMovie = -1;
        private bool modeMovieAdd = false;
        private bool modeMovieEdit = false;

        private void LoadMovies()
        {
            // LOAD PRICE CATEGORY
            cbPriceCategory.DisplayMember = "DisplayName";
            cbPriceCategory.ValueMember = "Id";
            cbPriceCategory.Items.AddRange(service.GetAllPriceCategories().ToArray());
        }

        private int AddMovie(Movie m)
        {
            int result = service.AddMovie(m);
            movies.Add(m);
            UpdateMovieLists();
            return result;
        }

        private int EditMovie(Movie m)
        {
            int result = service.EditMovie(m);
            movies[selectedMovie] = m;
            UpdateMovieLists();
            return result;
        }

        private int DeleteMovie(int id)
        {
            int result = service.DeleteMovie(id);
            movies.Remove(movies.Find(e => e.Id == id));
            UpdateMovieLists();
            return result;
        }

        private void ClearMovieFields()
        {
            txtMovieTitle.Text = "";
            txtMovieDuration.Text = "";
            txtMovieYear.Text = "";
            txtMovieActors.Text = "";
            txtMovieDirectors.Text = "";
            cbPriceCategory.SelectedIndex = -1;
        }

        private void EnableMovieFields(bool enabled)
        {
            txtMovieTitle.ReadOnly = !enabled;
            txtMovieDuration.ReadOnly = !enabled;
            txtMovieYear.ReadOnly = !enabled;
            txtMovieActors.ReadOnly = !enabled;
            txtMovieDirectors.ReadOnly = !enabled;
            cbPriceCategory.Enabled = enabled;
        }

        private void EnableMovieButtons(bool enabled)
        {
            bMovieAdd.Enabled = enabled;
            bMovieEdit.Enabled = enabled;
            bMovieDelete.Enabled = enabled;

            bMovieSave.Enabled = !enabled;
            bMovieCancel.Enabled = !enabled;
        }

        private bool ValidateMovieFields()
        {
            if (txtMovieTitle.Text == "")
            {
                lblMovieError.Visible = true;
                lblMovieError.Text = "'Titel' ikke udfyldt!";
                return false;
            }
            if (txtMovieDuration.Text == "")
            {
                lblMovieError.Visible = true;
                lblMovieError.Text = "'Spilletid' ikke udfyldt!";
                return false;
            }
            if (txtMovieYear.Text == "")
            {
                lblMovieError.Visible = true;
                lblMovieError.Text = "'Produktions år' ikke udfyldt!";
                return false;
            }
            if (txtMovieActors.Text == "")
            {
                lblMovieError.Visible = true;
                lblMovieError.Text = "'Skuespillere' ikke udfyldt!";
                return false;
            }
            if (txtMovieDirectors.Text == "")
            {
                lblMovieError.Visible = true;
                lblMovieError.Text = "'Instruktør' ikke udfyldt!";
                return false;
            }
            if (cbPriceCategory.SelectedIndex == -1)
            {
                lblMovieError.Visible = true;
                lblMovieError.Text = "'Pris kategori' ikke valgt!";
                return false;
            }

            return true;
        }

        private void bMovieAdd_Click(object sender, EventArgs e)
        {
            modeMovieAdd = true;

            ClearMovieFields();
            EnableMovieFields(true);
            EnableMovieButtons(false);

            txtMovieTitle.Focus();
        }

        private void bMovieEdit_Click(object sender, EventArgs e)
        {
            modeMovieEdit = true;

            EnableMovieFields(true);
            EnableMovieButtons(false);

            txtMovieTitle.Focus();
        }

        private void listMovies_SelectedIndexChanged(object sender, EventArgs e)
        {
            bool doUpdate = true;
            if (modeMovieAdd)
            {
                if (MessageBox.Show("Vil du afbryde tilføjelsen af ny film?", "Afbryd?", MessageBoxButtons.YesNo, MessageBoxIcon.Question) == System.Windows.Forms.DialogResult.No)
                    doUpdate = false;
            }
            else if (modeMovieEdit)
            {
                if (MessageBox.Show("Vil du afbryde redigeringen af film?", "Afbryd?", MessageBoxButtons.YesNo, MessageBoxIcon.Question) == System.Windows.Forms.DialogResult.No)
                    doUpdate = false;
            }


            if (doUpdate)
            {
                if (listMovies.SelectedIndex == -1)
                {
                    ClearMovieFields();
                }
                else
                {
                    modeMovieAdd = false;
                    modeMovieEdit = false;

                    EnableMovieFields(false);
                    EnableMovieButtons(true);


                    selectedMovie = listMovies.SelectedIndex;

                    Movie m = movies.Find(mov => mov.Id == ((Movie)listMovies.Items[selectedMovie]).Id);
                    txtMovieTitle.Text = m.Title;
                    txtMovieDuration.Text = m.Duration.ToString();
                    txtMovieYear.Text = m.Year.ToString();
                    txtMovieActors.Text = m.Actors;
                    txtMovieDirectors.Text = m.Directors;

                    for (int i = 0; i < cbPriceCategory.Items.Count; i++)
                    {
                        if (((PriceCategory)cbPriceCategory.Items[i]).Id == m.PriceCategory.Id)
                            cbPriceCategory.SelectedIndex = i;
                    }
                }
            }
        }

        private void bReload_Click(object sender, EventArgs e)
        {
            LoadMovies();
        }

        private void bMovieSave_Click(object sender, EventArgs e)
        {
            if (ValidateMovieFields())
            {
                if (modeMovieAdd)
                {
                    Movie m = new Movie()
                    {
                        Title = txtMovieTitle.Text,
                        Duration = int.Parse(txtMovieDuration.Text),
                        Year = int.Parse(txtMovieYear.Text),
                        Actors = txtMovieActors.Text,
                        Directors = txtMovieDirectors.Text,
                        PriceCategory = ((PriceCategory)cbPriceCategory.SelectedItem)
                    };

                    if (AddMovie(m) > 0)
                    {
                        ClearMovieFields();
                        EnableMovieFields(false);
                        EnableMovieButtons(true);
                        modeMovieAdd = false;
                        MessageBox.Show("Filmen '" + m.Title + "' er nu tilføjet!");
                    }
                    else
                    {
                        MessageBox.Show("FEJL: Kunne ikke tilføje filmen...");
                    }
                }
                else if (modeMovieEdit)
                {
                    int id = ((Movie)listMovies.Items[selectedMovie]).Id;
                    Movie m = new Movie()
                    {
                        Id = id,
                        Title = txtMovieTitle.Text,
                        Duration = int.Parse(txtMovieDuration.Text),
                        Year = int.Parse(txtMovieYear.Text),
                        Actors = txtMovieActors.Text,
                        Directors = txtMovieDirectors.Text,
                        PriceCategory = ((PriceCategory)cbPriceCategory.SelectedItem)
                    };

                    if (EditMovie(m) > 0)
                    {
                        ClearMovieFields();
                        EnableMovieFields(false);
                        EnableMovieButtons(true);
                        modeMovieEdit = false;
                        MessageBox.Show("Redigering fuldført!");
                    }
                    else
                    {
                        MessageBox.Show("FEJL: Kunne ikke redigere filmen...");
                    }
                }
                else
                {
                    MessageBox.Show("ERROR");
                }
            }

        }

        private void bMovieCancel_Click(object sender, EventArgs e)
        {
            modeMovieEdit = false;
            modeMovieAdd = false;

            listMovies.SelectedIndex = selectedMovie = -1;
            ClearMovieFields();
            EnableMovieFields(false);
            EnableMovieButtons(true);
        }

        private void bMovieDelete_Click(object sender, EventArgs e)
        {
            if (MessageBox.Show("Filmen vil blive slette! Er du sikker?", "Slet?", MessageBoxButtons.YesNo, MessageBoxIcon.Question) == System.Windows.Forms.DialogResult.Yes)
            {
                selectedMovie = listMovies.SelectedIndex;
                Movie m = movies.Find(mov => mov.Id == ((Movie)listMovies.Items[selectedMovie]).Id);
                DeleteMovie(m.Id);
            }
        }

        #endregion

        private void fieldsNumOnly_KeyDown(object sender, KeyEventArgs e)
        {
            e.SuppressKeyPress = Tools.ValidateKeyPressAsNumber(e);
        }

        private void cbPriceCategory_KeyPress(object sender, KeyPressEventArgs e)
        {
            e.Handled = true;
        }



    }
}
